package org.micode.libs.dao.base;

import lombok.extern.slf4j.Slf4j;
import org.micode.common.domain.base.BaseEntity;
import org.micode.common.domain.base.BaseQuery;
import org.micode.common.domain.vo.EditObjVO;
import org.micode.common.utils.BeanExtUtils;
import org.micode.common.utils.StringExtUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务类,基本方法
 */
@Slf4j
public abstract class BaseEditServiceImpl
        <KT, DTO extends BaseEntity<KT>, DO extends BaseEntity<KT>,
                QY extends BaseQuery<DO>, DAO extends BaseEditDao<DTO>>
        extends BaseInsertServiceImpl<KT, DTO, DO, QY, DAO>
        implements BaseEditService<KT, DTO, QY> {

    public BaseEditServiceImpl() {
        super();
    }

    protected void beforeUpdate(DTO entity) {
        // DTO nothing, the subclass can extends it for special process.
    }

    @Override
    public int delete(EditObjVO<KT> key) {
        EditObjVO<List<KT>> keys = new EditObjVO<>();
        List<KT> lst = new ArrayList<>();
        lst.add(key.getWantEdit());
        keys.setUpdUser(key.getUpdUser());
        keys.setUpdProg(key.getUpdProg());
        keys.setUpdIp(key.getUpdIp());
        keys.setWantEdit(lst);
        return deleteByKeys(keys);
    }

    @Override
    public int deleteByKeys(EditObjVO<List<KT>> delIds) {
        String ids = getKeysString(delIds.getWantEdit());
        if (StringExtUtils.isAnyEmpty(ids)) {
            return 0;
        }
        return getMyDao().deleteByKeys(ids);
    }

    @Override
    public int deleteByExample(DTO example) {
        if (BeanExtUtils.checkAllFieldValueIsNull(example, example.getClass().getSuperclass())) {
            // 如果example的所有属性都是null，则直接返回0，避免更新所有的数据
            return 0;
        }
        return getMyDao().deleteByExample(example);
    }

    protected String getKeysString(List<KT> keys) {
        if (null == keys || keys.size() <= 0) {
            return null;
        }

        KT key = keys.get(0);
        if (key instanceof Integer || key instanceof Long || key instanceof Short) {
            return StringExtUtils.getIdsStr(keys);
        } else if (key instanceof String) {
            return StringExtUtils.idsString(keys);
        }
        return null;
    }

    @Override
    public int update(DTO entity) {
        if (!BeanExtUtils.checkAllFieldValueIsNull(
                entity, entity.getClass().getSuperclass())) {
            beforeUpdate(entity);
            return getMyDao().update(entity);
        } else {
            log.info(entity.getClass().getSimpleName() + " | " +
                    entity.getClass().getSuperclass().getSimpleName() + " : " +
                    entity.toString());
            return 0;
        }
    }

    @Override
    public int updateByExample(DTO update, DTO example) {
        if (BeanExtUtils.checkAllFieldValueIsNull(example, example.getClass().getSuperclass())) {
            // 如果example的所有属性都是null，则直接返回0，避免更新所有的数据
            return 0;
        }
        return getMyDao().updateByExample(update, example);
    }

}
